// 登录模块
import signinTpl from '../views/signin.art'
import { signin as signinModel} from '../models/signin'
const htmlSignin = signinTpl({})

const _handleSubmit = (router) => {
  return async (e) => {
    e.preventDefault()
    // 提交表单
    const data = $('#signin').serialize()

    let { res, jqXHR } = await signinModel(data)

    const responseHeader = jqXHR.getResponseHeader('X-Access-Token')
    // console.log(responseHeader)
    localStorage.setItem('lg-token', responseHeader)
    if(res.ret) {
      router.go('/index/users')
    } else {
      alert(res.data.message)
    }
  }
}

const signin = (router) => {
  return (req, res, next) => {
    res.render(htmlSignin)
    // _handleSubmit(router)
    $('#signin').on('submit', _handleSubmit(router))
  }
}


export {
  signin
}